(function () {
  app
    .controller('conditionCtrl', function ($state, tradeApi, centerdilog, alertTip, $filter, $location, $stateParams, $scope, quoteApi, local_m, $rootScope, header_m, $interval) {
      $scope.condition_type = $stateParams.type;
      $scope.run_details = local_m.get('running');
      header_m.set({
        title_c: $scope.condition_type === '1'
          ? '条件单（创建策略）'
          : '条件单（卖出策略）'
      });

      var now = new Date();
      var years = now.getFullYear();
      var day = now.getDate();
      var month = now.getMonth();
      var hours = now.getHours();
      var mins = now.getMinutes();
      var seconds = now.getSeconds();

      $scope.req_data = {
        trigger_type: '1',
        trigger_direction: '2',
        cust_efficient_date:new Date(years, month, day, hours, mins, seconds),
        cust_efficient_time:new Date(years, month, day, hours, mins, seconds),
        efficient_date: '',
        efficient_time: ''

      }
      angular.extend($scope.req_data, $stateParams.data);

      $scope.ok = function () {
        $scope.req_data.efficient_date = $filter('date')($scope.req_data.cust_efficient_date, 'yyyy-MM-dd');
        $scope.req_data.efficient_time = $filter('date')($scope.req_data.cust_efficient_time, 'HH:mm:ss');
        
        if (!$scope.agree) {
          alertTip.open({msg: '您必须同意《服务协议》才能添加条件单'});
          return;
        }
        centerdilog.open({
          msg: $scope.condition_type === '1'
            ? '确定要添加条件单（创建策略）吗？'
            : '确定要添加条件单（卖出策略）吗？',
          ok: function () {
            tradeApi
              .query({
                funcId: $scope.condition_type === '1'
                  ? '300039'
                  : '300040',
                data: $scope.req_data
              })
              .then(function (res) {
                alertTip.open({msg: '条件单添加成功！', state: 1});
              });
          }
        });

      }

    });
})();